/**
 * Author: lonewolf
 * Date: 2014-10-05 17:23:59
 */
/**
 * 影片剪辑
 * 
 * @param {Array或image} images 图片参数，如果是image对象数组，则后面的三个参数不用填
 * @param {int} imageWidth 帧图片宽，如果images是image对象，则用填写
 * @param {int} imageHeight 帧图片高，同上
 * @param {int} num 帧图片总数，同上
 */
function LMoveClip (images,imageWidth,imageHeight,num) {
	LExtends(this, LSprite, []);
	var bitmapArr=[];
	if (images instanceof Array) {
		for (var i = 0; i < images.length; i++) {
			var bitmap=new LBitmap(new LBitmapData(images[i]));
			bitmapArr.push({frame:bitmap,label:"",func:null,params:null});
		};
	}
	else
	{
		var w=images.width;
		var h=images.height;
		var col=w/imageWidth;
		var row=h/imageHeight;
		if (row*col<num) {
			throw "LMoveClip: wrong images num";
			return;
		};
		for (var i = 0; i < row; i++) {
			for (var j = 0; j < col; j++) {
				if ((i*col+j)<num) {
					var bitmapData=new LBitmapData(images, j*imageWidth, i*imageHeight, imageWidth, imageHeight);
					var bitmap=new LBitmap(bitmapData)
					bitmapArr.push({frame:bitmap,label:"",func:null,params:null});
				};
			};
		};
	}
	// 帧信息列表 {frame:bitmap,label:"start",func:null,params:null}
	this.frameList=bitmapArr;
	this.currentFrame=1;
	this.currentFrameLabel="";
	this.currentIndex=0;
	this.totalFrames=this.frameList.length;
	// 播放速度(多少系统帧移动一movieclip帧)
	this.speed=1;
	this.playing=false;
	// 当前帧图片
	this.curImage=null;

	this.count=0;
	var e=new LSprite();
	this.addChild(e);
	e.addEventListener(LEvent.ENTER_FRAME, LMoveClip_onFrame);

	this._setFrame(0);
	this.play();
}
// 系统每帧执行函数
function LMoveClip_onFrame (event) {
	var mc=event.currentTarget.parent;
	if (!mc.playing) {
		return;
	};
	mc.count++;
	if (mc.count%mc.speed==0) {
		mc.count=0;
		var index=mc.currentIndex+1;
		if (index>=mc.totalFrames) {
			index=0;
		};
		mc._setFrame(index);
	};
}
// 内部方法，播放到某一帧
LMoveClip.prototype._setFrame = function(index) {
	var frameObj=this.frameList[index];
	this.currentFrameLabel=frameObj.label;
	this.currentFrame=index+1;
	this.currentIndex=index;
	if (this.curImage) {
		this.curImage.remove();
	};
	this.addChild(frameObj.frame);
	this.curImage=frameObj.frame;
	// 看看是否有要执行的帧函数
	if (frameObj.func) {
		frameObj.func(this,frameObj.params);
	};
};
/**
 * 添加帧脚本，播放到指定帧时执行某一函数
 *
 * @param {int或string} frame 帧序号或标签
 * @param {function} func 要执行的函数
 * @param {any} params 可选，func的参数列表，默认为null。注意，func的第一个参数是LMoveClip本身
 */
LMoveClip.prototype.addFrameScript = function(frame,func,params) {
	if (typeof(func)!="function") {
		throw "LMoveClip.addFrameScript: '"+func+"' is not a function";
		return;
	};
	var frameObj=null;
	if (typeof(frame)=="number") {
		if (frame>this.frameList.length||frame<1) {
			throw "LMoveClip.addFrameScript: frame "+frame+" out of range";
			return;
		}
		else
		{
			frameObj=this.frameList[frame-1];
		}
	}
	else if (typeof(frame)=="string") {
		for (var i = 0; i < this.frameList.length; i++) {
			var f=this.frameList[i];
			if (f.label==frame) {
				frameObj=f;
				break;
			};
		};
	};
	if (frameObj!=null) {
		if (!arguments[2]) {
			params=null;
		};
		frameObj.func=func;
		frameObj.params=params;
	}
	else
	{
		throw "LMoveClip.addFrameScript: no label name '"+frame+"'";
	}
};
/**
 * 给帧设置标签
 *
 * @param {string} label 标签
 * @param {int} frame 对应的帧序号，从1开始
 */
LMoveClip.prototype.setLable = function(label,frame) {
	if (frame>this.frameList.length||frame<1) {
		throw "LMoveClip.setLable: frame "+frame+" out of range";
		return;
	};
	this.frameList[frame-1].label=label;
};
/**
 * 开始播放
 */
LMoveClip.prototype.play = function() {
	if (!this.playing) {
		this.count=0;
		this.playing=true;
	};
};
/**
 * 结束播放
 */
LMoveClip.prototype.stop = function() {
	this.playing=false;
};
/**
 * 前一帧
 */
LMoveClip.prototype.prevFrame = function() {
	if (this.currentIndex==0) {
		// 当前帧是第一帧
		this._setFrame(this.totalFrames-1);
	}
	else
	{
		this._setFrame(this.currentIndex-1);
	}
};
/**
 * 下一帧
 */
LMoveClip.prototype.nextFrame = function() {
	if (this.currentIndex==this.totalFrames-1) {
		// 当前帧是最后一帧
		this._setFrame(0);
	}
	else
	{
		this._setFrame(this.currentIndex+1);
	}
};
/**
 * 从指定帧开始播放
 *
 * @param {int或string} frame 帧序号或标签
 */
LMoveClip.prototype.gotoAndPlay = function(frame) {
	if(typeof(frame)=="number")
	{
		if (frame<1) {
			frame=1;
		}
		else if (frame>this.totalFrames) {
			frame=this.totalFrames;
		};
		this._setFrame(frame-1);
		this.play();
	}
	else if (typeof(frame)=="string") {
		for (var i = 0; i < this.frameList.length; i++) {
			var frameObj=this.frameList[i];
			if (frameObj.label==frame) {
				this._setFrame(i);
				this.play();
				break;
			};
		};
	};
};
/**
 * 转到指定帧并停止播放
 *
 * @param {int或string} frame 帧序号或标签
 */
LMoveClip.prototype.gotoAndStop = function(frame) {
	if(typeof(frame)=="number")
	{
		if (frame<1) {
			frame=1;
		}
		else if (frame>this.totalFrames) {
			frame=this.totalFrames;
		};
		this._setFrame(frame-1);
		this.stop();
	}
	else if (typeof(frame)=="string") {
		for (var i = 0; i < this.frameList.length; i++) {
			var frameObj=this.frameList[i];
			if (frameObj.label==frame) {
				this._setFrame(i);
				this.stop();
				break;
			};
		};
	};
};
/**
 * 当前帧的编号，从1开始
 */
LMoveClip.prototype.getCurrentFrame = function() {
	return this.currentFrame;
};
/**
 * 当前帧对应的标签
 */
LMoveClip.prototype.getCurrentFrameLabel = function() {
	return this.currentFrameLabel;
};
/**
 * 帧总数
 */
LMoveClip.prototype.getTotalFrames = function() {
	return this.totalFrames;
};
/**
 * 设置播放速度，多少系统帧向前一movieclip帧，默认为1
 *
 * @param {int} speed
 */
LMoveClip.prototype.setSpeed = function(speed) {
	speed=parseInt(speed+"");
	if (isNaN(speed)||speed<=0) {
		speed=1;
	};
	this.speed=speed;
};
/**
 * 获取播放速度
 */
LMoveClip.prototype.getSpeed = function() {
	return this.speed;
};
/**
 * 是否在播放中
 */
LMoveClip.prototype.isPlaying = function() {
	return this.playing;
};